layui.use(['form', 'layer','laydate','upload'], function(){
	  var form = layui.form
	  ,layer = layui.layer
	  ,laydate = layui.laydate
	  ,upload = layui.upload
	  ,$ = layui.$;
	  	
	var usersId = user.usersId;
	var usersType = user.usersType;
	
	if(usersType == '2'){
		$(".hide").attr("style","width:300px;");
	}
	
	var data = {"usersId":usersId};
	var url = "../user/findById.action";
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			form.val("myform", {
				  "usersId": msg.usersId
				  ,"usersName": msg.usersName
				  ,"usersPassword": msg.usersPassword
				  ,"usersImage": msg.usersImage
				  ,"usersPhone": msg.usersPhone
				  ,"usersSex": msg.usersSex
				  ,"businessName": msg.businessName
				  ,"fullName": msg.fullName
				  ,"usersSfz": msg.usersSfz
				})
				$('#demo1').attr('src', "../"+msg.usersImage); //图片链接（base64）
		}
	});
	  
	//普通图片上传
	  var uploadInst = upload.render({
	    elem: '#test1'
	    ,url: '../upload/uploadFile.action'
	    ,accept: 'images'
	    ,choose: function(obj){
	    	//预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
	        obj.preview(function(index, file, result){
	          $('#demo1').attr('src', result); //图片链接（base64）
	        });
	    }
	    ,done: function(res){
	      $("#usersImage").val(res.url);
	    }
	    ,error: function(){
	      //演示失败状态，并实现重传
	      var demoText = $('#demoText');
	      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
	      demoText.find('.demo-reload').on('click', function(){
	        uploadInst.upload();
	      });
	    }
	  });
	  
	  form.on('radio(usersSex)', function(data){
		  $("#usersSex").val(data.value);
	});  
	
	  $("#editOrUpdate").click(function(){
		  var obj = event.target;
		  var value = $(obj).text();
			if(value == "编辑"){
				$(".layui-input").removeAttr("disabled");
				$("#test1").removeAttr("disabled");
				$("#editOrUpdate").text("保存");
			}else {
				var usersName = $("#usersName").val();
				var usersPassword = $("#usersPassword").val();
				if(usersName == "" || usersName == null){
					alert("用户名称不能为空");
				}else if(usersPassword == "" || usersPassword == null){
					alert("用户密码不能为空");
				}
				
				var data = $("#myform").serialize();
				var url = "../user/saveOrUpdate.action";
				$.ajax({
					url:url,
					data:data,
					success:function(msg){
						if(msg.state == '1'){
							layer.msg("保存成功",{icon:1,time:1000},function(){
								parent.location.reload();
							});
						}else {
							layer.msg("保存失败",{icon:2,time:1000});
						}
					}
				});
				
				$(".layui-input").attr("disabled","disabled");
				$("#test1").attr("disabled","disabled");
				$("#editOrUpdate").text("编辑");
			}
	  });
	  
	//限制手机号码数字输入
	  $("#usersPhone").keyup(function(event){
			var obj = event.target;
			var phone = $(obj).val();
			//限制只能输入数字的正则表达式
			var reg = new RegExp("^[0-9]*$");
			if(!reg.test(phone)){
				$(obj).val("");
				return false;
				//限制长度
			}else if(phone.length > 11){
				$(obj).val(phone.substring(0,11));
			}
		})
	//手机号码正则表达式
	  $("#usersPhone").blur(function(){
		var obj = event.target;
		var phone = $(obj).val();
		//验证输入有效的手机号码
		if(!(/^1[34578]\d{9}$/.test(phone))){
	    	layer.msg('请输入有效的手机号码!', {icon:5,time:1000,anim:6});
	    	$(obj).val("");
	        return false; 
	    }
	  });
	  
	  //限制身份证号数字英文输入
	  $("#usersSfz").keyup(function(event){
		  var obj = event.target;
		  var sfz = $(obj).val();
		//限制只能输入数字的正则表达式
			var reg = new RegExp("^[0-9]*$");
			if(!reg.test(sfz)){
				$(obj).val("");
				return false;
				//限制长度
			}else if(sfz.length > 18){
				$(obj).val(sfz.substring(0,18));
			}
	  })
	  
	  //身份证号正则表达式
	  $("#usersSfz").blur(function(){
		  var obj = event.target;
			var sfz = $(obj).val();
			var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
			//验证输入有效的手机号码
			if(!reg.test(sfz)){
		    	layer.msg('请输入有效的身份证号码!', {icon:5,time:1000,anim:6});
		    	$(obj).val("");
		        return false; 
		    }
	  });
	  
});